package com.enqualcomm.kids.activities;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.base.BaseActivity;
import com.enqualcomm.kids.base.MyResultReceiver;
import com.enqualcomm.kids.component.InitContextIntentService;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.mvp.NetworkModel;
import com.enqualcomm.kids.mvp.login.wx.presenter.WXRegisterPresenter;
import com.enqualcomm.kids.mvp.login.wx.view.BindPhoneView;
import com.enqualcomm.kids.mvp.login.wx.presenter.Presenter;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import common.utils.MyLogger;
import common.utils.PlatformUtil;
import common.utils.PromptUtil;

/**
 * Created by Administrator on 2016/9/7.
 */
@EActivity(R.layout.activity_bind_phone_new)
public class BindPhoneActivity extends BaseActivity implements BindPhoneView {

    private boolean isOldUser = false;
    private NetworkModel networkModel;

    Presenter presenter;
    @ViewById(R.id.new_user_btn)
    TextView new_user_btn;
    @ViewById(R.id.bind_old_use_btn)
    TextView bind_old_use_btn;
    @ViewById(R.id.phone_number_et)
    EditText phone_number_et;
    @ViewById(R.id.password_et)
    EditText password_et;
    @ViewById(R.id.bind_phone_notice_tv)
    TextView bind_phone_notice_tv;
    @ViewById(R.id.country_regin_tv)
    TextView country_regin_tv;
    @ViewById(R.id.title_bar_title_tv)
    TextView title_bar_title_tv;
    @ViewById(R.id.fill_phone_ll)
    LinearLayout fill_phone_ll;
    @ViewById(R.id.done_btn)
    Button done_btn;


    @AfterViews
    void setupView() {
        networkModel = new NetworkModel();
        presenter = new WXRegisterPresenter(this);
        TextView title_bar_terminal_name_tv = (TextView) findViewById(R.id.title_bar_terminal_name_tv);
        View title_bar_terminal_icon_iv = findViewById(R.id.title_bar_terminal_icon_iv);
        title_bar_terminal_icon_iv.setVisibility(View.GONE);
        title_bar_terminal_name_tv.setVisibility(View.VISIBLE);
        title_bar_terminal_name_tv.setText(getString(R.string.system_settings));
//        title_bar_title_tv.setText(R.string.bind_phone_account_dong);
        title_bar_terminal_name_tv.setText(R.string.bind_phone_title);
//        title_bar_title_tv.setText(R.string.bind_phone_title);
        setButtonBG(true);
    }

    @Click(R.id.title_bar_left_iv)
    void goback() {
        finish();
    }

    @Click(R.id.bind_old_use_btn)
    void chooseOld() {
        isOldUser = true;
        setButtonBG(false);
    }

    @Click(R.id.new_user_btn)
    void chooseNew() {
        isOldUser = false;
        setButtonBG(true);
    }

    @Click(R.id.done_btn)
    void done() {
        showProgress();
        presenter.start();
    }

    @Override
    public void wxRegistSuccess(boolean isSuccess) {
        if (isSuccess) {
            loading();
        } else {
            PromptUtil.toast(this, "注册失败，请稍后再试");
        }

    }

    @Override
    public void bindOldAccountSuccess(boolean isSuccess) {
        hideProgress();
        if (isSuccess) {
            loading();
        } else {
            PromptUtil.toast(this, R.string.account_password_wrong);
        }
    }

    @Override
    public String getPhoneNumber() {
        return phone_number_et.getText().toString().trim();
    }

    @Override
    public String getCountryCode() {
        String conutryCode = country_regin_tv.getText().toString().trim();
        if (conutryCode.equals(getString(R.string.watch_numer_country_regin))) {
            conutryCode = "";
        } else if (!TextUtils.isEmpty(conutryCode)) {
            conutryCode = conutryCode.substring(1, conutryCode.length());
        }
        return conutryCode;
    }

    @Override
    public String getPassword() {
        return password_et.getText().toString().trim();
    }

    @Override
    public void showMessage(String message) {

    }

    @Override
    public void showMessage(int stringID) {
        PromptUtil.toast(this, stringID);
    }

    @Override
    public boolean getRegistType() {
        return isOldUser;
    }

    private void loading() {
        showProgress();
        Intent intent = new Intent(this, InitContextIntentService.class);
        intent.putExtra(InitContextIntentService.RECEIVER, new MyResultReceiver(new Handler(), this));
        startService(intent);
    }

    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        hideProgress();
        MyLogger.kLog().i("login=============" + resultCode);
        if (resultCode == InitContextIntentService.SUCCESS) {
            jumpToMainAct();
        } else {
            PromptUtil.toast(getApplicationContext(), R.string.account_password_wrong);
        }
    }

    /**
     * 跳转到主页面
     */
    private void jumpToMainAct() {
        int windowWidth = PlatformUtil.windowWidth(this);
        int windowHeight = PlatformUtil.windowHeight(this);
        AppDefault appDefault = new AppDefault();
        appDefault.setWindowWidth(windowWidth);
        appDefault.setWindowHeight(windowHeight);

        startActivity(MainActivity_.class);
        finish();
    }

    @Click(R.id.country_regin_tv)
    void chooseCountryCode() {
        startActivityForResult(new Intent(this, ChooseCountryActivity.class), 10000);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10000 && resultCode == 10001) {
            String countryCode = data.getStringExtra("number");
            String name = data.getStringExtra("name");

            country_regin_tv.setText(name);
            if (countryCode.length() > 1) {
                country_regin_tv.setText(countryCode);
            } else {
                country_regin_tv.setText("");
            }
        }
    }

    private void setButtonBG(boolean newUserEnable) {
        new_user_btn.setSelected(newUserEnable);
        bind_old_use_btn.setSelected(!newUserEnable);
        fill_phone_ll.setSelected(!newUserEnable);
        if (newUserEnable) {
            password_et.setVisibility(View.GONE);
            phone_number_et.setHint(R.string.bind_phone_new_hit);
            bind_phone_notice_tv.setText(R.string.bind_phone_notice_3);
            done_btn.setText(R.string.new_bind_done_btn_text);
        } else {
            password_et.setVisibility(View.VISIBLE);
            phone_number_et.setHint(R.string.new_bind_phone_old_hit);
            bind_phone_notice_tv.setText(R.string.bind_phone_notice_2);
            done_btn.setText(R.string.new_bind_bind_btn_text);
        }

    }
}
